function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 获取缓存
 * @param {*} key 
 */
function getCache(key){
  return wx.getStorageSync(key);
}

/**
 * 设置缓存
 * @param {*} key 
 * @param {*} val 
 */
function setCache(key,val){
  wx.setStorageSync(key, val)
}

/**
 * 请求 post | get
 * @param {*} url 
 * @param {*} data 
 * @param {*} type 
 */
function requests(url,data,type,success){
  var userinfo = this.getCache('userinfo');
  var token = this.getCache('token')
  if (!token) {
    var token = {token:0}
  }
  wx.request({
    url: url,
    data: data,
    method: type,
    header: { 'content-type': 'application/x-www-form-urlencoded',token:token.token},
    success: res=> {
      success(res)
    }
  })
}

module.exports = {
  formatTime: formatTime,
  getCache:getCache,
  setCache:setCache,
  requests:requests
}
